# name : BE analysis using proc mixed, for BE CV wr
# key : be.cv.wr.mixed
# contributor: Shuguang Sun
# --
* Dataset containing REFERENCE observations;
data ref1 (KEEP = seq subj lat1r)
ref2 (KEEP = seq subj lat2r);
set pk;
if trt = 'R';
/* 4-way */
/* TRTR, RTRT */
if (seq = 1 and per = 2) or (seq = 2 and per = 1) then do;
lat1r = lauct;
output ref1;
end;
else if (seq = 1 and per = 4) or (seq = 2 and per = 3) then do;
lat2r = lauct;
output ref2;
end;

/* 3-way */
/* TRR, RTR, RRT */
if (seq = 1 and per = 2) or (seq = 2 and per = 1) or (seq = 3 and per = 1) then do;
lat1r = lauct;
output ref1;
end;
else if (seq = 1 and per = 3) or (seq = 2 and per = 3) or (seq = 3 and per = 2) then do;
lat2r = lauct;
output ref2;
end;

run;

data ref;
merge ref1 ref2;
by seq subj;
dlat = lat1r - lat2r;
run;

* Intermediate analysis -- dlat;
proc mixed data=ref;
class seq;
model dlat = seq;
estimate 'average' intercept 1 seq 0.5 0.5 / e cl alpha=0.1;
ods output CovParms=dout1;
ods output Estimates=dout2;
ods output NObs=dout3;
title1 'scaled average BE';
title2 'intermediate analysis - dlat, mixed';
run;

* From the dataset DOUT1, calculate the following DOUT1;
data dout1;
set dout1;
s2wr = estimate / 2;
run;

data dout2;
set dout2;
dfd = df;
run;

* From the above parameters, calculate the final 95 % upper confidence bound;
data result;
merge dout1 dout2;
sWR = sqrt(s2wr);
run;